package com.by.cart.controller;


import com.by.cart.entity.Cart;
import com.by.cart.entity.SelectionDto;
import com.by.cart.service.CartService;
import com.by.common.core.bean.ResultBean;
import com.by.common.core.response.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车管理")
@RestController
@RequestMapping("/api/v1")
public class CartController {


    @Autowired
    CartService cartService;

    /**
     * 添加商品到购物车或者更新商品数量
     * @param cart
     * @return
     */
    @Operation(summary = "添加商品到购物车或者更新商品数量")
    @PostMapping("/cart/add")
    public ResultBean addCart(@RequestParam Long userID,@RequestBody Cart cart){

        cartService.addCart(userID,cart);
        return R.success("添加成功");
    }

    /**
     * 删除购物车商品
     * @param userID
     * @param skuID
     * @return
     */
    @Operation(summary = "删除购物车商品")
    @DeleteMapping("/cart/delete")
    public ResultBean deleteCart(@RequestParam Long userID,@RequestParam Long skuID){
        cartService.removeCart(userID,skuID);
        return R.success("删除成功");
    }

    /**
     * 更新购物车商品规格
     * @param userID
     * @param skuID
     * @return
     */
    @Operation(summary = "更新购物车商品规格")
    @PutMapping("/cart/update")
    public ResultBean updateCart(@RequestParam Long userID,@RequestParam Long skuID ,@RequestParam Long newSkuID){
        cartService.updateCart(userID,skuID,newSkuID);
        return R.success("更新成功");
    }

    /**
     * 查询购物车
     * @param userID
     * @return
     */
    @Operation(summary = "查询所有购物车")
    @GetMapping("/cart/list")
    public ResultBean getCart(@RequestParam Long userID){
        List<Cart> cartList = cartService.getCarts(userID);
        return R.success("查询成功",cartList);
    }

    /**
     * 清空购物车
     * @param userID
     * @return
     */
    @Operation(summary = "清空购物车")
    @DeleteMapping("/cart/clear")
    public ResultBean clearCart(@RequestParam Long userID){
        cartService.clearCart(userID);
        return R.success("清空成功");
    }

    /**
     * 全选/全不选
     * @param userID
     * @param selected
     * @return
     */
    @Operation(summary = "全选/全不选")
    @PutMapping("/cart/selectAll")
    public ResultBean selectAll(@RequestParam Long userID , @RequestParam boolean selected){
        cartService.selectAll(userID,selected);
        return R.success("操作成功");
    }

    /**
     * 更新商品选中状态
     * @param userID
     * @param selectionDto
     * @return
     */
    @Operation(summary = "更新商品选中状态")
    @PutMapping("/cart/updateSelection")
    public ResultBean updateSelection(@RequestParam Long userID , @RequestBody SelectionDto selectionDto) {
        for (int i = 0; i <selectionDto.getSkuIds().size(); i++) {
            cartService.updateCartSelection(userID, selectionDto.getSkuIds().get(i), selectionDto.isSelected());
        }

        return R.success("操作成功");
    }




    /**
     * 获取购物车中商品数量
     * @param userId
     * @return
     */
    @Operation(summary = "获取购物车中商品数量")
    @GetMapping("/cart/getCartnum")
    public ResultBean getCartnum(@RequestParam Long userId) {
        int cartnum = cartService.getCartnum(userId);
        return R.success("操作成功", cartnum);
    }

    /**
     * 将购物车数据持久化到数据库
     * @param
     * @return
     */
    @Operation(summary = "将购物车数据持久化到数据库")
    @PostMapping("/cart/saveToMysql")
    public ResultBean saveToMysql(@RequestBody List<Long> userId){

        for (int i = 0; i < userId.size(); i++) {
            cartService.persistToDatabase(userId.get(i));
        }
        return R.success("操作成功");

    }


}
